package com.gitee.wsl.net.ssdp.description.xml

data class XmlElement (
    var tag: String,
    var attributes: Map<String, String>? = null,
    var value: String? = null,
    var children: List<XmlElement>? = null
){
    inline fun <reified T> get() : T = getOrNull() ?: throw IllegalArgumentException("$tag is required")

    inline fun <reified T> getOrNull() : T? = value?.let{ s: String ->
        when (T::class.simpleName){
            "Int" -> s.toInt()
            "Double" -> s.toDouble()
            "Float" -> s.toFloat()
            "Long" -> s.toLong()
            "Short" -> s.toShort()
            "Byte" -> s.toByte()
            "Boolean" -> s.toBoolean()
            else -> s
        } as T
    }

}